import Vue from "vue";
import Vuex from "vuex";
import axios from "axios";

Vue.use(Vuex);

let tenant_json = localStorage.getItem('tenant') ? JSON.parse(localStorage.getItem('tenant')) : "";

export default new Vuex.Store({

  state: {
    Authorization: localStorage.getItem('Authorization') ? localStorage.getItem('Authorization') : "",
    usertype: localStorage.getItem('usertype') ? localStorage.getItem('usertype') : "",
    is_active: localStorage.getItem('is_active') ? localStorage.getItem('is_active') : "",
    username: localStorage.getItem('username') ? localStorage.getItem("username") : "",
    unionid: localStorage.getItem('unionid') ? localStorage.getItem("unionid") : "",
    mode: localStorage.getItem('mode') ? localStorage.getItem("mode") : "",
    wx_token: localStorage.getItem('wx_token') ? localStorage.getItem("wx_token") : "",
    tenant: {
      id: tenant_json.id ? tenant_json.id : "",
      house_id: tenant_json.house_id ? tenant_json.house_id : "",
      building_id: tenant_json.building_id ? tenant_json.building_id : "",
      openid: tenant_json.openid ? tenant_json.openid : "",
      user_openid:tenant_json.user_openid ? tenant_json.user_openid : "",
    },
    extra_tenant_user_openid:localStorage.getItem('extra_tenant_user_openid') ? localStorage.getItem("extra_tenant_user_openid") : "",
  },
  mutations: {
    loginChange(state, user) {
      state.username = user.username;
      state.token = user.token;
      state.usertype = user.usertype;
      state.is_active = user.is_active;
      state.unionid = user.unionid;
      state.mode = user.mode;
      localStorage.setItem('username', user.username);
      localStorage.setItem('usertype', user.usertype);
      localStorage.setItem('is_active', user.is_active);
      localStorage.setItem('Authorization', user.token);
      localStorage.setItem('unionid', user.unionid);
      localStorage.setItem('mode', user.mode);
    },
    loginOut(state) {
      state.Authorization = "";
      state.is_active = "";
      state.usertype = "";
      state.unionid = "";
      state.mode = "";
      state.tenant = "";
      localStorage.setItem('Authorization', "");
      localStorage.setItem('usertype', "");
      localStorage.setItem('is_active', "");
      localStorage.setItem('unionid', "");
      localStorage.setItem('mode', "");
      localStorage.setItem('tenant', "");
      axios.post("/api/adminapi/signout")
        .then(res => {})
        .catch(err => {})
    },
    resetUnionid(state, unionid) {
      state.unionid = unionid;
      localStorage.setItem('unionid', unionid);
    },
    resetOpenid(state, openid) {
      state.tenant.openid = openid;
      localStorage.setItem('tenant', JSON.stringify({openid:openid}));
    },
    setTenant(state, tenant) {

      state.tenant.id = tenant.id;
      state.tenant.house_id = tenant.house_id;
      state.tenant.building_id = tenant.building_id;
      state.tenant.openid = tenant.openid;
      state.tenant.user_openid=tenant.user_openid;

      localStorage.setItem('tenant', JSON.stringify({
        id: tenant.id,
        house_id: tenant.house_id,
        building_id: tenant.building_id,
        openid: tenant.openid,
        user_openid:tenant.user_openid
      }));

    },
  },
  actions: {
    login(context, user) {
      context.commit('loginChange', user);
    },
    loginOut(context) {
      context.commit('loginOut');
    },
    resetUnionid(context, unionid) {
      context.commit('resetUnionid', unionid);
    },
    resetOpenid(context, openid) {
      context.commit('resetOpenid', openid);
    },
    setTenant(context, tenant) {
      context.commit('setTenant', tenant);
    },
  }
});
